﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using DomainModel.Ninject;
using EsbMonitor.Ninject;
using Ninject;
using NsbMonitorClientService;
using NsbMonitorClientService.Mapping;
using NsbMonitorClientService.QueueManager;

namespace NsbMonitorClientServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {

            new AutoMapperInitialization().Init();

            KernelContainer.Kernel = new StandardKernel();

            KernelContainer.Kernel.Load(typeof(DataLayerNinjectModule).Assembly);

            KernelContainer.Kernel.Bind<IQueueReader>().To<BasicQueueReader>();
            KernelContainer.Kernel.Bind<IQueueSender>().To<BasicQueueSender>();
            KernelContainer.Kernel.Bind<IQueuePurger>().To<BasicQueuePurger>();
            KernelContainer.Kernel.Bind<IFetchQueueStatus>().To<FetchQueueStatus>();
            KernelContainer.Kernel.Bind<IExploreMsMq>().To<ExploreMsMq>();
            KernelContainer.Kernel.Bind<ICountMessagesInQueue>().To<CountMessagesInQueue>();
            KernelContainer.Kernel.Bind<IFetchOldestMessageTime>().To<FetchOldestMessageTime>();

            Uri baseAddress = new Uri("http://localhost:7777/");
            using (ServiceHost host = new ServiceHost(typeof(NsbMonitorClientServiceImpl),baseAddress))
            {

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                NetNamedPipeBinding netNamedPipeBinding = new NetNamedPipeBinding();
                Binding mex = MetadataExchangeBindings.CreateMexHttpBinding();

                host.AddServiceEndpoint(typeof(INsbMonitorClientService), netNamedPipeBinding, "net.pipe://NsbMonitorClientService");
                host.AddServiceEndpoint(typeof(IMetadataExchange), mex, "http://localhost:7777/NsbMonitorClientService/mex");

                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }


        }
    }
}
